🚀 O Cenário

Supply Chain Chaos 📉

Ships are arriving in a scrambled order. We need to calculate the Métrica de Caos (number of inversions) to optimize the AI traffic controller.

O que é uma Inversão?

A pair of indices $(i, j)$ is an inversion if:

  • $i < j$ (O navio $i$ chega antes do $j$)
  • $A[i] > A[j]$ (O navio $i$ tem um ID de prioridade mais alto)

Analysis 🔍

Sequência de Exemplo: [2, 4, 1, 3, 5]

  • (2, 1): 2 chega antes de 1, mas 2 > 1
  • (4, 1): 4 chega antes de 1, mas 4 > 1
  • (4, 3): 4 chega antes de 3, mas 4 > 3
  • Caos Total: 3 Inversões

O Desafio

A brute force nested loop is $O(N^2)$.

for i in range(n):
  for j in range(i+1, n): ...

For $N=100,000$, this takes ~10 billion ops. Resultado: Tempo Limite Excedido.